package com.easyidea.its.web.action.role;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

import com.easyidea.its.domain.model.system.Permission;
import com.easyidea.its.domain.model.system.Role;
import com.easyidea.its.facade.role.RoleMgr;
import com.easyidea.its.util.ItsConstants;
import com.easyidea.its.util.PageUtil;
import com.easyidea.its.util.Result;
import com.easyidea.its.web.action.base.ItsBaseAction;
import com.opensymphony.xwork.Action;

/**
 * 
 * @author kangning.hu
 * 
 */
public class RoleAction extends ItsBaseAction {

	/**
	 * 
	 */
	private static final long serialVersionUID = -5383160807054208294L;

	// private Logger log = Logger.getLogger(this.getClass());

	private RoleMgr roleMgr;

	private Result<Role> platformRoleResult;

	private Integer[] permissionIds;

	private List<Integer> permissionListIds;

	private Role platformRole;

	private Integer status = 1;

	private Integer roleId;

	private String roleName;

	/**
	 * @return the roleName
	 */
	public String getRoleName() {
		return roleName;
	}

	/**
	 * @param roleName
	 *            the roleName to set
	 */
	public void setRoleName(String roleName) {
		this.roleName = roleName;
	}

	public String initRoleList() {
		getRoleList();
		return SUCCESS;
	}

	public String initAddRole() {
		return SUCCESS;
	}

	public String saveRole() {
		if (isExistSameName(platformRole.getRoleName(), platformRole.getId())) {
//			setErrorMsg(ActionOperation.ROLE_NAME_EXIST_SAME_NAME.getI18n());
			return INPUT;
		}
		this.roleMgr.savePlatformRole(platformRole, permissionIds);
//		successMsgKey = StringUtil.toUtf8String(ActionOperation.ADDNEW_SUCCESS?.getI18n());
		getSession().setAttribute(ItsConstants.SUCCESS_MSG, "save.success");
		return SUCCESS;
	}

	public List<Permission> getPermissionList(Integer parentId) {
		return roleMgr.getAllPerssionsByParentId(parentId);
	}

	public String viewRole() {
		platformRole = roleMgr.getPlatformRoleById(roleId);
		if (platformRole.getPermissionList() != null) {
			permissionListIds = new ArrayList<Integer>();
			for (Permission p : platformRole.getPermissionList()) {
				permissionListIds.add(p.getId());
			}
		}
		return SUCCESS;
	}

	public String modifyRole() {
		Role tmPlatformRole = roleMgr.getPlatformRoleById(platformRole.getId());
		if (tmPlatformRole == null) {
//			setErrorMsgKey("el.admin.error.platformrole.noexists");
			return ERROR;
		}
		if (isExistSameName(platformRole.getRoleName(), platformRole.getId())) {
			errorMsg = "角色名称存在重复！";
			return INPUT;
		}
		this.roleMgr.updatePlatformRole(platformRole, permissionIds);
//		successMsgKey = StringUtil.toUtf8String(ActionOperation.UPDATE_SUCCESS.getI18n());
		getSession().setAttribute(ItsConstants.SUCCESS_MSG, "save.success");
		return SUCCESS;
	}

	public String deleteRole() {
		try {
			
			this.roleMgr.deletePlatformRoleById(roleId);
			getSession().setAttribute("successMsg", "delete.success");
		} catch (Exception e) {
//			setErrorMsgKey("el.role.operation.delete.error");
			getSession().setAttribute("successMsg", "delete.role.failure");
		}
//		successMsgKey = StringUtil.toUtf8String(ActionOperation.DELETE_SUCCESS.getI18n());
		return Action.SUCCESS;
	}

	private boolean isExistSameName(String name, Integer id) {
		if (roleMgr.isExistSameName(name, id)) {
//			setErrorMsg(ActionOperation.ROLE_NAME_EXIST_SAME_NAME.getI18n());
			return true;
		}
		return false;
	}

	/**
	 * 
	 */
	@SuppressWarnings("unchecked")
	private void getRoleList() {
		int totalRecords = this.roleMgr.getPlatformRoleCountByStatus(roleName,
				status);
		if (totalRecords > 0) {
			int start = (this.currentPage - 1) * this.getPageSize();
			Collection<Role> records = roleMgr.getPlatformRolesByStatus(
					roleName, status, start, this.getPageSize());
			platformRoleResult = PageUtil.createResult(this.getPageSize(),
					currentPage, totalRecords, records);
		} else {
			platformRoleResult = PageUtil.createResult(this.getPageSize(),
					currentPage, 0, new ArrayList());
		}
	}

	public RoleMgr getRoleMgr() {
		return roleMgr;
	}

	public void setRoleMgr(RoleMgr roleMgr) {
		this.roleMgr = roleMgr;
	}

	/**
	 * @return the permissionIds
	 */
	public Integer[] getPermissionIds() {
		return permissionIds;
	}

	/**
	 * @param permissionIds
	 *            the permissionIds to set
	 */
	public void setPermissionIds(Integer[] permissionIds) {
		this.permissionIds = permissionIds;
	}

	/**
	 * @return the platformRole
	 */
	public Role getPlatformRole() {
		return platformRole;
	}

	/**
	 * @param platformRole
	 *            the platformRole to set
	 */
	public void setPlatformRole(Role platformRole) {
		this.platformRole = platformRole;
	}

	/**
	 * @return the roleId
	 */
	public Integer getRoleId() {
		return roleId;
	}

	/**
	 * @param roleId
	 *            the roleId to set
	 */
	public void setRoleId(Integer roleId) {
		this.roleId = roleId;
	}

	/**
	 * @return the platformRoleResult
	 */
	public Result<Role> getPlatformRoleResult() {
		return platformRoleResult;
	}

	/**
	 * @param platformRoleResult
	 *            the platformRoleResult to set
	 */
	public void setPlatformRoleResult(Result<Role> platformRoleResult) {
		this.platformRoleResult = platformRoleResult;
	}

	/**
	 * @return the status
	 */
	public Integer getStatus() {
		return status;
	}

	/**
	 * @param status
	 *            the status to set
	 */
	public void setStatus(Integer status) {
		this.status = status;
	}

	/**
	 * @return the permissionListIds
	 */
	public List<Integer> getPermissionListIds() {
		return permissionListIds;
	}

	/**
	 * @param permissionListIds
	 *            the permissionListIds to set
	 */
	public void setPermissionListIds(List<Integer> permissionListIds) {
		this.permissionListIds = permissionListIds;
	}
}
